package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin,LocalDateTime end) {
        //新增用户数
        Map map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        Integer newUsers = reportMapper.getUserByMap(map);
        Integer status = null;
        map.put("status",status);
        Integer totalOrderCount = reportMapper.getOrdersByMap(map);
        status = Orders.COMPLETED;
        map.put("status",status);
        //有效订单数
        Integer validOrderCount= reportMapper.getOrdersByMap(map);
        //营业额
        Double turnover = reportMapper.getByMap(map);
        //订单完成率
        Double orderCompletionRate=0.0;
        if(totalOrderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
        }
        //平均客单价
        Double unitPrice=0.0;
        if (validOrderCount !=0){
            unitPrice = turnover.doubleValue()/validOrderCount;
        }
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    @Override
    public OrderOverViewVO getOverViewOrders() {
        LocalDate localDate = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(localDate, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(localDate, LocalTime.MAX);
        Integer status = null;
        Map map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        //全部订单数
        map.put("status",status);
        Integer allOrders = reportMapper.getOrdersByMap(map);
        //已取消订单数
        status= Orders.CANCELLED;
        map.put("status",status);
        Integer cancelledOrders= reportMapper.getOrdersByMap(map);
        //已完成订单数
        status = Orders.COMPLETED;
        map.put("status",status);
        Integer completedOrders = reportMapper.getOrdersByMap(map);
        //带派送订单数量
        status= Orders.CONFIRMED;
        map.put("status",status);
        Integer deliveredOrders = reportMapper.getOrdersByMap(map);
        //待接单订单数量
        status = Orders.TO_BE_CONFIRMED;
        map.put("status",status);
        Integer waitingOrders = reportMapper.getOrdersByMap(map);
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();

    }

    @Override
    public DishOverViewVO getOverViewDishes() {
        //停售菜品数
        Integer status= StatusConstant.DISABLE;
        Integer discontinued = dishMapper.countStatus(status);
        //起售菜品数
        status= StatusConstant.ENABLE;
        Integer sold = dishMapper.countStatus(status);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public SetmealOverViewVO getOverViewSetmeals() {
        //停售套餐数
        Integer status= StatusConstant.DISABLE;
        Integer discontinued = setmealMapper.countStatus(status);
        //起售套餐数
        status= StatusConstant.ENABLE;
        Integer sold = setmealMapper.countStatus(status);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }
}



